package it.polimi.authentication.web.beans;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import it.polimi.TravelDream.BasePackageDTO;
import it.polimi.TravelDream.ExcursionDTO;
import it.polimi.TravelDream.FlightDTO;
import it.polimi.TravelDream.HotelDTO;
import it.polimi.TravelDream.PackagejoinDTO;
import it.polimi.TravelDream.SavedPackageDTO;
import it.polimi.TravelDream.PackageMgr;
import it.polimi.TravelDream.SearchEngine;
import it.polimi.TravelDream.UserMgr;
import it.polimi.TravelDream.packageExLinkerDTO;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;





@ManagedBean(name="savedPackageBean")
@SessionScoped
public class SavedPackageBean {
	
	@EJB
	private PackageMgr packageMgr;
	
    @EJB
	private SearchEngine searchEngine;
    
    @EJB
    private UserMgr usermgr;

	private SavedPackageDTO savedPackage;
	
	private SavedPackageDTO tempSavedPackage;
	
	private BasePackageDTO basePackage;

	private HotelDTO selectedHotel;	
	private FlightDTO selectedFlight;
	private FlightDTO selectedRetFlight;
	private ExcursionDTO selectedExcursion;
	
	private boolean outFlight;
	private boolean retFlight;
	private boolean hotel;
 
	private String temp;

	private List<FlightDTO> filteredList;
	private List<FlightDTO> filteredRetList;
	private List<HotelDTO> filteredHotList;
	private List<ExcursionDTO> filteredExcList;
	private List<ExcursionDTO> selectedExcursionsList;
	
	private boolean renderFlight;
	private boolean renderRetFlight;
	private boolean renderHotel;
	private boolean renderExcursion;
	
	private int packID;
	
	private int iWantPayFlight;
	private int iWantPayRetFlight;
	private int iWantPayHotel;
	private int iWantPayExcursions;
	private boolean creationPage;
	private int joinedPeople;
	private int excToBePaid;
	
	private String namePayer;
	private String messagePayer;

	public SavedPackageBean(){
		selectedExcursionsList = new ArrayList<ExcursionDTO>();
		savedPackage = new SavedPackageDTO();
		filteredList = new ArrayList<FlightDTO>();
		filteredList.clear();
		filteredRetList = new ArrayList<FlightDTO>();
		filteredRetList.clear();
		filteredHotList = new ArrayList<HotelDTO>();
		filteredHotList.clear();
		filteredExcList = new ArrayList<ExcursionDTO>();
		filteredExcList.clear();

	}
	
	public String createSavedFromOutFlight(){
		savedPackage = new SavedPackageDTO();
		tempSavedPackage = new SavedPackageDTO();
		renderExcursion=false;
		renderFlight=false;
		renderHotel=false;
		renderRetFlight=false;
		savedPackage.setDepLocation(selectedFlight.getDepLocation());
		savedPackage.setArrLocation(selectedFlight.getArrLocation());
		savedPackage.setDepDate(selectedFlight.getDate_Time());
		savedPackage.setFlight1(selectedFlight.getId());
		savedPackage.setCreatorPeople(1);
		tempSavedPackage.setDepDate(savedPackage.getDepDate());
		tempSavedPackage.setRetDate(savedPackage.getRetDate());
		tempSavedPackage.setArrLocation(savedPackage.getArrLocation());
		tempSavedPackage.setDepLocation(savedPackage.getDepLocation());
		selectedExcursionsList.clear();
		outFlight=true;
		hotel=false;
		retFlight=false;
		return "PackageCreation?faces-redirect=true";
	}
	
	public String createSavedFromRetFlight(){
		savedPackage = new SavedPackageDTO();
		tempSavedPackage = new SavedPackageDTO();
		renderExcursion=false;
		renderFlight=false;
		renderHotel=false;
		renderRetFlight=false;
		savedPackage.setDepLocation(selectedRetFlight.getArrLocation());
		savedPackage.setArrLocation(selectedRetFlight.getDepLocation());
		savedPackage.setRetDate(selectedRetFlight.getDate_Time());
		savedPackage.setFlight2(selectedRetFlight.getId());
		savedPackage.setCreatorPeople(1);
		tempSavedPackage.setDepDate(savedPackage.getDepDate());
		tempSavedPackage.setRetDate(savedPackage.getRetDate());
		tempSavedPackage.setArrLocation(savedPackage.getArrLocation());
		tempSavedPackage.setDepLocation(savedPackage.getDepLocation());
		selectedExcursionsList.clear();
		outFlight=false;
		hotel=false;
		retFlight=true;
		return "PackageCreation?faces-redirect=true";
	}
	
	public String createSavedFromHotel(){
		savedPackage = new SavedPackageDTO();
		tempSavedPackage = new SavedPackageDTO();
		renderExcursion=false;
		renderFlight=false;
		renderHotel=false;
		renderRetFlight=false;
		savedPackage.setHotel(selectedHotel.getId());
		savedPackage.setArrLocation(selectedHotel.getCity());
		savedPackage.setCreatorPeople(1);
		tempSavedPackage.setArrLocation(savedPackage.getArrLocation());
		selectedExcursionsList.clear();
		outFlight=false;
		hotel=true;
		retFlight=false;
		return "PackageCreation?faces-redirect=true";
	}
	
	public String createSavedFromExcursion(){
		savedPackage = new SavedPackageDTO();
		tempSavedPackage = new SavedPackageDTO();
		renderExcursion=false;
		renderFlight=false;
		renderHotel=false;
		renderRetFlight=false;
		selectedExcursionsList.clear();
		savedPackage.setCreatorPeople(1);
		savedPackage.setArrLocation(selectedExcursion.getCity());
		tempSavedPackage.setArrLocation(savedPackage.getArrLocation());
		selectedExcursionsList.add(selectedExcursion);
		outFlight=false;
		hotel=false;
		retFlight=false;
		return "PackageCreation?faces-redirect=true";		
		
	}
	
	public String createSavedFromBasePackage(){
		savedPackage = new SavedPackageDTO();
		tempSavedPackage = new SavedPackageDTO();
		renderExcursion=false;
		renderFlight=false;
		renderHotel=false;
		renderRetFlight=false;
		selectedExcursionsList.clear();
		savedPackage.setCreatorPeople(1);
		this.modifySavedPackageFromBasePackage();
		return "PackageCreation?faces-redirect=true";
	}
	
	public String insertSavedPackage() {
		packageMgr.saveSavedPackage(savedPackage,selectedExcursionsList);
		return "home?faces-redirect=true";
	}
	
	public String modifyDepLoc(){
		tempSavedPackage.setArrLocation(savedPackage.getArrLocation());
		tempSavedPackage.setDepDate(savedPackage.getDepDate());
		tempSavedPackage.setRetDate(savedPackage.getRetDate());
		tempSavedPackage.setCreatorPeople(savedPackage.getCreatorPeople());
		savedPackage.setDepLocation(tempSavedPackage.getDepLocation());
		if (selectedFlight!= null && selectedFlight.getDepLocation()!=null && !selectedFlight.getDepLocation().equals(savedPackage.getDepLocation())){
			selectedFlight=null;
			outFlight=false;
			savedPackage.setFlight1(-1);
		}
		if (selectedRetFlight!= null && selectedRetFlight.getArrLocation()!=null && !selectedRetFlight.getArrLocation().equals(savedPackage.getDepLocation())){
			selectedRetFlight=null;
			retFlight=false;
			savedPackage.setFlight2(-1);
		}
		if (creationPage==true) return "PackageModification?faces-redirect=true";
		else return "PackageCreation?faces-redirect=true";
		
	}
	
	public String modifyArrLoc(){
		tempSavedPackage.setDepLocation(savedPackage.getDepLocation());
		tempSavedPackage.setDepDate(savedPackage.getDepDate());
		tempSavedPackage.setRetDate(savedPackage.getRetDate());
		tempSavedPackage.setCreatorPeople(savedPackage.getCreatorPeople());
		savedPackage.setArrLocation(tempSavedPackage.getArrLocation());
		if (selectedFlight!= null && selectedFlight.getArrLocation()!=null && !selectedFlight.getArrLocation().equals(savedPackage.getArrLocation())){
			selectedFlight=null;
			outFlight=false;
			savedPackage.setFlight1(-1);
		}
		if (selectedRetFlight!= null &&selectedRetFlight.getDepLocation()!=null && !selectedRetFlight.getDepLocation().equals(savedPackage.getArrLocation())){
			selectedRetFlight=null;
			retFlight=false;
			savedPackage.setFlight2(-1);
		}
		if(selectedHotel!=null && selectedHotel.getCity()!=null && !selectedHotel.getCity().equals(savedPackage.getArrLocation())){
			selectedHotel=null;
			hotel= false;
			savedPackage.setHotel(-1);			
		}
		if(!selectedExcursionsList.isEmpty())
			if(!selectedExcursionsList.get(0).getCity().equals(savedPackage.getArrLocation())){
			selectedExcursionsList.clear();				
			}
		
		if (creationPage==true) return "PackageModification?faces-redirect=true";
		else return "PackageCreation?faces-redirect=true";
		
	}
	
	public String modifyDate(){
		if(savedPackage.getRetDate()!=null && tempSavedPackage.getDepDate()!=null)
		{
			if (tempSavedPackage.getDepDate().before(savedPackage.getRetDate()))
				{
				tempSavedPackage.setDepLocation(savedPackage.getDepLocation());
				tempSavedPackage.setArrLocation(savedPackage.getArrLocation());
				tempSavedPackage.setRetDate(savedPackage.getRetDate());
				tempSavedPackage.setCreatorPeople(savedPackage.getCreatorPeople());
				savedPackage.setDepDate(tempSavedPackage.getDepDate());
				if (selectedFlight!= null){
					selectedFlight=null;
					outFlight=false;
					savedPackage.setFlight1(-1);
				}
				if(!selectedExcursionsList.isEmpty() && savedPackage.getDepDate()!=null){
					for (Iterator<ExcursionDTO> it =selectedExcursionsList.iterator();it.hasNext();){
						ExcursionDTO excursionDTO = it.next();
						if (excursionDTO.getDate().before(savedPackage.getDepDate()))	
							it.remove();					
				}}
				}
			else
				{tempSavedPackage.setDepDate(savedPackage.getDepDate());
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Warning", "The departure date must be setted before the return date"));  
				if (creationPage==true) return "PackageModification?";
				else return "PackageCreation?";
				}
			}
		else 
			{
			tempSavedPackage.setDepLocation(savedPackage.getDepLocation());
			tempSavedPackage.setArrLocation(savedPackage.getArrLocation());
			tempSavedPackage.setRetDate(savedPackage.getRetDate());
			tempSavedPackage.setCreatorPeople(savedPackage.getCreatorPeople());
			savedPackage.setDepDate(tempSavedPackage.getDepDate());
			if (selectedFlight!= null){
				selectedFlight=null;
				outFlight=false;
				savedPackage.setFlight1(-1);
			}
			if(!selectedExcursionsList.isEmpty() && savedPackage.getDepDate()!=null){
				for (Iterator<ExcursionDTO> it =selectedExcursionsList.iterator();it.hasNext();){
					ExcursionDTO excursionDTO = it.next();
					if (excursionDTO.getDate().before(savedPackage.getDepDate()))	
						it.remove();					
					}

				}
			}
		
		if (creationPage==true) return "PackageModification?faces-redirect=true";
		else return "PackageCreation?faces-redirect=true";
		
	}
	
	public String modifyTotPeople(){
		tempSavedPackage.setDepLocation(savedPackage.getDepLocation());
		tempSavedPackage.setArrLocation(savedPackage.getArrLocation());
		tempSavedPackage.setDepDate(savedPackage.getDepDate());
		tempSavedPackage.setRetDate(savedPackage.getRetDate());
		savedPackage.setCreatorPeople(savedPackage.getCreatorPeople());
		
		if (creationPage==true) return "PackageModification?faces-redirect=true";
		else return "PackageCreation?faces-redirect=true";
		
	}
	
	public String modifyRetDate(){
		if(savedPackage.getDepDate()!= null && tempSavedPackage.getRetDate()!=null)
		{
			if(tempSavedPackage.getRetDate().after(savedPackage.getDepDate())){
				tempSavedPackage.setDepLocation(savedPackage.getDepLocation());
				tempSavedPackage.setArrLocation(savedPackage.getArrLocation());
				tempSavedPackage.setDepDate(savedPackage.getDepDate());
				tempSavedPackage.setCreatorPeople(savedPackage.getCreatorPeople());
				savedPackage.setRetDate(tempSavedPackage.getRetDate());
				if (selectedRetFlight!= null){
					selectedRetFlight=null;
					retFlight=false;
					savedPackage.setFlight2(-1);
				}
				if(!selectedExcursionsList.isEmpty() && savedPackage.getRetDate()!=null){
					for (Iterator<ExcursionDTO> it =selectedExcursionsList.iterator();it.hasNext();){
						ExcursionDTO excursionDTO = it.next();
						if (excursionDTO.getDate().after(savedPackage.getRetDate()))	
							it.remove();					
						}
					}
				}
				else 
					{tempSavedPackage.setRetDate(savedPackage.getRetDate());
					 FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Warning", "The return date must be setted after the departure date"));  
					 if (creationPage==true) return "PackageModification?";
						else return "PackageCreation?";
					}
			}
		else 
			{
			tempSavedPackage.setDepLocation(savedPackage.getDepLocation());
			tempSavedPackage.setArrLocation(savedPackage.getArrLocation());
			tempSavedPackage.setDepDate(savedPackage.getDepDate());
			tempSavedPackage.setCreatorPeople(savedPackage.getCreatorPeople());
			savedPackage.setRetDate(tempSavedPackage.getRetDate());
			if (selectedRetFlight!= null){
				selectedRetFlight=null;
				retFlight=false;
				savedPackage.setFlight2(-1);
			}
			if(!selectedExcursionsList.isEmpty() && savedPackage.getRetDate()!=null){
				for (Iterator<ExcursionDTO> it =selectedExcursionsList.iterator();it.hasNext();){
					ExcursionDTO excursionDTO = it.next();
					if (excursionDTO.getDate().after(savedPackage.getRetDate()))	
						it.remove();					
				}
			}
		}
		
		if (creationPage==true) return "PackageModification?faces-redirect=true";
		else return "PackageCreation?faces-redirect=true";
	}
	
	public void modifySavedPackage(){
		tempSavedPackage = new SavedPackageDTO();
		renderExcursion=false;
		renderFlight=false;
		renderHotel=false;
		renderRetFlight=false;
		tempSavedPackage.setDepDate(savedPackage.getDepDate());
		tempSavedPackage.setRetDate(savedPackage.getRetDate());
		tempSavedPackage.setArrLocation(savedPackage.getArrLocation());
		tempSavedPackage.setDepLocation(savedPackage.getDepLocation());
		tempSavedPackage.setCreatorPeople(savedPackage.getCreatorPeople());
		iWantPayExcursions = 0;
		iWantPayFlight = 0;
		iWantPayHotel = 0;
		iWantPayRetFlight = 0;
		if(savedPackage.getFlight1()==-1){
			outFlight=false;
			selectedFlight=null;
		}
		else{
			outFlight=true;
			tempSavedPackage.setFlight1ToBeP(savedPackage.getFlight1ToBeP());
			selectedFlight=searchEngine.searchFlightByID(savedPackage.getFlight1());
		}
		if(savedPackage.getFlight2()==-1){
			retFlight=false;
			selectedRetFlight=null;
		}
		else{
			retFlight=true;
			tempSavedPackage.setflight2ToBeP(savedPackage.getflight2ToBeP());
			selectedRetFlight=searchEngine.searchFlightByID(savedPackage.getFlight2());
		}
		if(savedPackage.getHotel()==-1){
			hotel=false;
			selectedHotel=null;
		}
		else{
			hotel=true;
			tempSavedPackage.setHotToBeP(savedPackage.getHotToBeP());
			selectedHotel=searchEngine.searchHotelByID(savedPackage.getHotel());
		}
		selectedExcursionsList.clear();
		selectedExcursionsList=searchEngine.searchExcursionBySavedPackageID(savedPackage.getId());
		if(!selectedExcursionsList.isEmpty())excToBePaid = packageMgr.searchExcToBePaid(savedPackage.getId());
	}
	
	public void modifySavedPackageFromBasePackage(){
		renderExcursion=false;
		renderFlight=false;
		renderHotel=false;
		renderRetFlight=false;
		savedPackage.setArrLocation(basePackage.getArrLocation());
		savedPackage.setDepLocation(basePackage.getDepLocation());
		savedPackage.setDepDate(basePackage.getDepDate());
		savedPackage.setRetDate(basePackage.getRetDate());
		savedPackage.setFlight1(basePackage.getOutFlightID());
		savedPackage.setFlight2(basePackage.getRetFlightID());
		savedPackage.setHotel(basePackage.getHotelID());
		tempSavedPackage = new SavedPackageDTO();
		tempSavedPackage.setDepDate(savedPackage.getDepDate());
		tempSavedPackage.setRetDate(savedPackage.getRetDate());
		tempSavedPackage.setArrLocation(savedPackage.getArrLocation());
		tempSavedPackage.setDepLocation(savedPackage.getDepLocation());
		tempSavedPackage.setCreatorPeople(savedPackage.getCreatorPeople());
		if(savedPackage.getFlight1()==-1){
			outFlight=false;
			selectedFlight=null;
		}
		else{
			outFlight=true;
			tempSavedPackage.setFlight1ToBeP(savedPackage.getFlight1ToBeP());
			selectedFlight=searchEngine.searchFlightByID(savedPackage.getFlight1());
		}
		if(savedPackage.getFlight2()==-1){
			retFlight=false;
			selectedRetFlight=null;
		}
		else{
			retFlight=true;
			tempSavedPackage.setflight2ToBeP(savedPackage.getflight2ToBeP());
			selectedRetFlight=searchEngine.searchFlightByID(savedPackage.getFlight2());
		}
		if(savedPackage.getHotel()==-1){
			hotel=false;
			selectedHotel=null;
		}
		else{
			hotel=true;
			tempSavedPackage.setHotToBeP(savedPackage.getHotToBeP());
			selectedHotel=searchEngine.searchHotelByID(savedPackage.getHotel());
		}
		selectedExcursionsList.clear();
		selectedExcursionsList=searchEngine.searchExcursionByBasePackageID(basePackage.getId());
	}
	
	public String searchOutFlight() {
		FlightDTO flight = new FlightDTO();
		flight.setDate_Time(savedPackage.getDepDate());
		flight.setArrLocation(savedPackage.getArrLocation());
		flight.setDepLocation(savedPackage.getDepLocation());
		filteredList.clear();
		filteredList = searchEngine.searchFlightDTO(flight);
		if (savedPackage.getRetDate()!=null){
			for (Iterator<FlightDTO> it =filteredList.iterator();it.hasNext();){
				FlightDTO flightDTO = it.next();
				if (flightDTO.getDate_Time().after(savedPackage.getRetDate()))	
					it.remove();		
			}
		}
		renderRetFlight=false;
		renderFlight=true;
		renderHotel=false;
		renderExcursion=false;
		if (creationPage==true) return "PackageModification?faces-redirect=true";
		else return "PackageCreation?faces-redirect=true";
	}
	
	public String searchRetFlight() {
		FlightDTO flight = new FlightDTO();
		flight.setDate_Time(savedPackage.getRetDate());
		flight.setArrLocation(savedPackage.getDepLocation());
		flight.setDepLocation(savedPackage.getArrLocation());
		filteredRetList.clear();
		filteredRetList = searchEngine.searchFlightDTO(flight);
		if (savedPackage.getDepDate()!=null){
			for (Iterator<FlightDTO> it =filteredRetList.iterator();it.hasNext();){
				FlightDTO flightDTO = it.next();
				if (flightDTO.getDate_Time().before(savedPackage.getDepDate()))	
					it.remove();		
			}
		}
		renderRetFlight=true;
		renderFlight=false;
		renderHotel=false;
		renderExcursion=false;
		if (creationPage==true) return "PackageModification?faces-redirect=true";
		else return "PackageCreation?faces-redirect=true";
	}
	
	public String searchHotel() {
		HotelDTO hotel = new HotelDTO();
		hotel.setCity(savedPackage.getArrLocation());
		filteredHotList.clear();
		filteredHotList = searchEngine.searchHotelDTO(hotel);
		renderRetFlight=false;
		renderFlight=false;
		renderHotel=true;
		renderExcursion=false;
		if (creationPage==true) return "PackageModification?faces-redirect=true";
		else return "PackageCreation?faces-redirect=true";
	}
	
	public String searchExcursion() {
		ExcursionDTO excursion = new ExcursionDTO();
		excursion.setCity(savedPackage.getArrLocation());
		excursion.setDate(savedPackage.getDepDate());
		filteredExcList.clear();
		filteredExcList = searchEngine.searchExcursionDTO(excursion);
		renderRetFlight=false;
		renderFlight=false;
		renderHotel=false;
		renderExcursion=true;
		if (savedPackage.getRetDate()!=null){
			for (Iterator<ExcursionDTO> it =filteredExcList.iterator();it.hasNext();){
				ExcursionDTO excursionDTO = it.next();
				if (excursionDTO.getDate().after(savedPackage.getRetDate()))	
					it.remove();		
			}
		}
		if (creationPage==true) return "PackageModification?faces-redirect=true";
		else return "PackageCreation?faces-redirect=true";
	}
	
	public String changeOutFlight() {
		savedPackage.setFlight1(selectedFlight.getId());
		outFlight=true;
		filteredList.clear();
		if (creationPage==true) return "PackageModification?faces-redirect=true";
		else return "PackageCreation?faces-redirect=true";
	}
	
	public String changeRetFlight() {
		savedPackage.setFlight2(selectedRetFlight.getId());
		retFlight=true;
		filteredRetList.clear();
		if (creationPage==true) return "PackageModification?faces-redirect=true";
		else return "PackageCreation?faces-redirect=true";
	}
	
	public String changeHotel() {
		savedPackage.setHotel(selectedHotel.getId());
		hotel=true;
		filteredHotList.clear();
		if (creationPage==true) return "PackageModification?faces-redirect=true";
		else return "PackageCreation?faces-redirect=true";
	}
	
	public String addExcursion() {
		if (!selectedExcursionsList.contains(selectedExcursion))
		selectedExcursionsList.add(selectedExcursion);
		if (creationPage==true) return "PackageModification?faces-redirect=true";
		else return "PackageCreation?faces-redirect=true";
	}
	
	public String removeOutFlight(){
		savedPackage.setFlight1(-1);
		selectedFlight=null;
		outFlight=false;
		if (creationPage==true) return "PackageModification?faces-redirect=true";
		else return "PackageCreation?faces-redirect=true";
	}
	
	public String removeRetFlight(){
		savedPackage.setFlight2(-1);
		selectedRetFlight=null;
		retFlight=false;
		if (creationPage==true) return "PackageModification?faces-redirect=true";
		else return "PackageCreation?faces-redirect=true";
	}
	
	public String removeHotel(){
		savedPackage.setHotel(-1);
		selectedHotel=null;
		hotel=false;
		if (creationPage==true) return "PackageModification?faces-redirect=true";
		else return "PackageCreation?faces-redirect=true";
	}
	
	public String removeExcursion(){
		selectedExcursionsList.remove(selectedExcursion);
		if (creationPage==true) return "PackageModification?faces-redirect=true";
		else return "PackageCreation?faces-redirect=true";
	}
	
	public String searchSavedPackagebyID(){
		
		savedPackage = searchEngine.searchSavedPackageByID(packID);	
			if (savedPackage == null )
				return "searchError?faces-redirect=true";
			else if (usermgr.getCurrentUserDTO() != null && savedPackage.getOwnerId() == usermgr.getCurrentUserDTO().getID())
				{	this.modifySavedPackage();
					if (savedPackage.getConfirmed() == false ) 
							return "/users/PackageModification?faces-redirect=true";
							
					else return "/users/PackageDetailPayment?faces-redirect=true";
				}
			else{
				this.modifySavedPackage();
				if (savedPackage.getConfirmed() == false ) return "/users/ShowPackageNotMineNC?faces-redirect=true";
				else return "/users/PackageDetailPayment?faces-redirect=true";			
			}
			
		}
		
		public String searchSavedOtherPackagebyID(){
			
			savedPackage = searchEngine.searchSavedPackageByID(packID);	
				if (savedPackage == null )
					return "/searchError?faces-redirect=true";
				if (savedPackage.getConfirmed() == true ) 
				{		
				
					if (savedPackage.getFlight1()!= -1){
							selectedFlight = searchEngine.searchFlightByID(savedPackage.getFlight1());
					}
					if (savedPackage.getFlight2()!= -1){
						selectedRetFlight = searchEngine.searchFlightByID(savedPackage.getFlight2());
					}
					if (savedPackage.getHotel()!= -1){
						selectedHotel = searchEngine.searchHotelByID(savedPackage.getHotel());
					}
						this.modifySavedPackage();
						return "PackageDetailPayment?faces-redirect=true";
				}
				else 
					{this.modifySavedPackage();
					return "ShowPackage?faces-redirect=true";}							
			}
		
		public String selectionBPUnlogged(){
			this.modifySavedPackageFromBasePackage();
			return "ShowPackage?faces-redirect=true";
		}
	

		public String visualizeMyPackage(){
		
		tempSavedPackage = new SavedPackageDTO();
		tempSavedPackage.setDepDate(savedPackage.getDepDate());
		tempSavedPackage.setRetDate(savedPackage.getRetDate());
		tempSavedPackage.setArrLocation(savedPackage.getArrLocation());
		tempSavedPackage.setDepLocation(savedPackage.getDepLocation());
		tempSavedPackage.setCreatorPeople(savedPackage.getCreatorPeople());
		iWantPayExcursions = 0;
		iWantPayFlight = 0;
		iWantPayHotel = 0;
		iWantPayRetFlight = 0;
		if(savedPackage.getFlight1()==-1){
			outFlight=false;
			selectedFlight=null;
		}
		else{
			outFlight=true;
			tempSavedPackage.setFlight1ToBeP(savedPackage.getFlight1ToBeP());
			selectedFlight=searchEngine.searchFlightByID(savedPackage.getFlight1());
		}
		if(savedPackage.getFlight2()==-1){
			retFlight=false;
			selectedRetFlight=null;
		}
		else{
			retFlight=true;
			tempSavedPackage.setflight2ToBeP(savedPackage.getflight2ToBeP());
			selectedRetFlight=searchEngine.searchFlightByID(savedPackage.getFlight2());
		}
		if(savedPackage.getHotel()==-1){
			hotel=false;
			selectedHotel=null;
		}
		else{
			hotel=true;
			tempSavedPackage.setHotToBeP(savedPackage.getHotToBeP());
			selectedHotel=searchEngine.searchHotelByID(savedPackage.getHotel());
		}
		selectedExcursionsList.clear();
		selectedExcursionsList=searchEngine.searchExcursionBySavedPackageID(savedPackage.getId());
		if(!selectedExcursionsList.isEmpty())excToBePaid = packageMgr.searchExcToBePaid(savedPackage.getId());
		
		
		if (savedPackage.getConfirmed() == true) return "PackageDetailPayment?faces-redirect=true";
		else return "/users/PackageModification?faces-redirect=true";
	}	
		
	public String pay(){

		int totalPaied = iWantPayFlight * (this.outFlightCost()) + 
						iWantPayRetFlight * (this.retFlightCost()) +
						iWantPayHotel * (this.hotelTotalCost()) +
						iWantPayExcursions * (this.excTotalCost());
						
		//--------CALL TO EXTERNAL PAYMENT SYSTEM: THE FOLLOWING CODE WILL APPLY ONLY IF THE PAYMENT WAS SUCCEDED--------//
		//PAYMENT RESULT == TRUE
		if(iWantPayFlight != 0)
			savedPackage.setFlight1ToBeP(savedPackage.getFlight1ToBeP() - this.iWantPayFlight);
		if(iWantPayRetFlight != 0)
			savedPackage.setflight2ToBeP(savedPackage.getflight2ToBeP() - this.iWantPayRetFlight);
		if(iWantPayHotel!=0)
			savedPackage.setHotToBeP(savedPackage.getHotToBeP() - this.iWantPayHotel);
		if(iWantPayExcursions!=0)
			excToBePaid = excToBePaid - iWantPayExcursions;
		packageMgr.payment(savedPackage, totalPaied, messagePayer, namePayer, excToBePaid);
		
		
		return "/paymentSucceded.xhtml?faces-redirect=true";
        //	END OF POSITIVE PAYMENT PROCESS
	}
	
	public int excTotalCost(){
		int tot=0;
		if (!selectedExcursionsList.isEmpty())
		{for(ExcursionDTO e : selectedExcursionsList){
			tot = tot + e.getPrice();
		}}
		return tot;
	}
	
	public int hotelTotalCost(){
		int tot = 0;
		if (selectedHotel!=null) tot = (int) (((savedPackage.getRetDate().getTime() - savedPackage.getDepDate().getTime()) / (1000*60*60*24) ) * selectedHotel.getPrice_Night());
		return tot;
	}
	
	public int outFlightCost(){
		
		if (selectedFlight!=null) return selectedFlight.getPrice();
		else return 0;
	}
	
	public int retFlightCost(){
			
			if (selectedRetFlight!=null) return selectedRetFlight.getPrice();
			else return 0;
		}
	
	public String joinPackage(){
		boolean alreadyJoined;  
		alreadyJoined = packageMgr.checkIfJoined(usermgr.getCurrentUserDTO().getID(), savedPackage.getId());
		if(alreadyJoined == false){
		PackagejoinDTO joinDTO = new PackagejoinDTO();
		joinDTO.setJoinedPeople(this.joinedPeople);
		joinDTO.setPackID(savedPackage.getId());
		joinDTO.setJoiner(usermgr.getCurrentUserDTO().getID());
		savedPackage.setTotPeople(savedPackage.getTotPeople() + this.joinedPeople);
		packageMgr.insertNewJoin(joinDTO);
		packageMgr.updateSavedPackage(savedPackage, selectedExcursionsList);
		
		return "home.xhtml?faces-redirect=true";
		}
		else return "joinError.xhtml?faces-redirect=true";
	}
	
	public String confirmSavedPackage(){
		if(savedPackage.getHotel()!=-1 && (savedPackage.getDepDate()==null || savedPackage.getRetDate()==null))
			{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Warning", "The package must have a package and return date in order to book the hotel!"));
			return "packageModification.xhtml?";
			}
		packageMgr.confirmSavedPackage(savedPackage, selectedExcursionsList);
		
		return "home.xhtml?faces-redirect=true";
	}
	
	public String updateSavedPackage(){
		packageMgr.updateSavedPackage(savedPackage, selectedExcursionsList);
		return "/users/home.xhtml?faces-redirect=true";
	}

	public HotelDTO getSelectedHotel() {
		return selectedHotel;
	}

	public void setSelectedHotel(HotelDTO selectedHotel) {
		this.selectedHotel = selectedHotel;
	}

	public FlightDTO getSelectedFlight() {
		return selectedFlight;
	}

	public void setSelectedFlight(FlightDTO selectedFlight) {
		this.selectedFlight = selectedFlight;
	}

	public FlightDTO getSelectedRetFlight() {
		return selectedRetFlight;
	}

	public void setSelectedRetFlight(FlightDTO selectedRetFlight) {
		this.selectedRetFlight = selectedRetFlight;
	}

	public SavedPackageDTO getSavedPackage() {
		return savedPackage;
	}

	public void setSavedPackage(SavedPackageDTO savedPackage) {
		this.savedPackage = savedPackage;
	}

	public String getTemp() {
		return temp;
	}

	public void setTemp(String temp) {
		this.temp = temp;
	}

	public boolean isOutFlight() {
		return outFlight;
	}

	public void setOutFlight(boolean outFlight) {
		this.outFlight = !(this.outFlight);
	}

	public boolean isRetFlight() {
		return retFlight;
	}

	public void setRetFlight(boolean retFlight) {
		this.retFlight = retFlight;
	}

	public boolean isHotel() {
		return hotel;
	}

	public void setHotel(boolean hotel) {
		this.hotel = hotel;
	}
	
	public List<FlightDTO> getFilteredList() {
		return filteredList;
	}

	public void setFilteredList(List<FlightDTO> filteredList) {
		this.filteredList = filteredList;
	}

	public List<FlightDTO> getFilteredRetList() {
		return filteredRetList;
	}

	public void setFilteredRetList(List<FlightDTO> filteredRetList) {
		this.filteredRetList = filteredRetList;
	}

	public List<HotelDTO> getFilteredHotList() {
		return filteredHotList;
	}

	public void setFilteredHotList(List<HotelDTO> filteredHotList) {
		this.filteredHotList = filteredHotList;
	}

	public List<ExcursionDTO> getFilteredExcList() {
		return filteredExcList;
	}

	public void setFilteredExcList(List<ExcursionDTO> filteredExcList) {
		this.filteredExcList = filteredExcList;
	}

	public List<ExcursionDTO> getSelectedExcursionsList() {
		return selectedExcursionsList;
	}

	public void setSelectedExcursionsList(List<ExcursionDTO> selectedExcursionsList) {
		this.selectedExcursionsList = selectedExcursionsList;
	}

	public void setSelectedExcursion(ExcursionDTO selectedExcursion) {
		this.selectedExcursion = selectedExcursion;
	}

	public ExcursionDTO getSelectedExcursion() {
		return selectedExcursion;
	}

	public boolean isRenderFlight() {
		return renderFlight;
	}

	public void setRenderFlight(boolean renderFlight) {
		this.renderFlight = renderFlight;
	}

	public boolean isRenderRetFlight() {
		return renderRetFlight;
	}

	public void setRenderRetFlight(boolean renderRetFlight) {
		this.renderRetFlight = renderRetFlight;
	}

	public boolean isRenderHotel() {
		return renderHotel;
	}

	public void setRenderHotel(boolean renderHotel) {
		this.renderHotel = renderHotel;
	}

	public boolean isRenderExcursion() {
		return renderExcursion;
	}

	public void setRenderExcursion(boolean renderExcursion) {
		this.renderExcursion = renderExcursion;
	}

	public SavedPackageDTO getTempSavedPackage() {
		return tempSavedPackage;
	}

	public void setTempSavedPackage(SavedPackageDTO tempSavedPackage) {
		this.tempSavedPackage = tempSavedPackage;
	}

	public int getPackID() {
		return packID;
	}

	public void setPackID(int packID) {
		this.packID = packID;
	}

	public int getiWantPayFlight() {
		return iWantPayFlight;
	}

	public void setiWantPayFlight(int iWantPayFlight) {
		this.iWantPayFlight = iWantPayFlight;
	}

	public int getiWantPayRetFlight() {
		return iWantPayRetFlight;
	}

	public void setiWantPayRetFlight(int iWantPayRetFlight) {
		this.iWantPayRetFlight = iWantPayRetFlight;
	}

	public int getiWantPayHotel() {
		return iWantPayHotel;
	}

	public void setiWantPayHotel(int iWantPayHotel) {
		this.iWantPayHotel = iWantPayHotel;
	}

	public int getiWantPayExcursions() {
		return iWantPayExcursions;
	}

	public void setiWantPayExcursions(int iWantPayExcursions) {
		this.iWantPayExcursions = iWantPayExcursions;
	}
	
	public void setcreationPage(boolean value){
		this.creationPage = value;
	}
	
	public boolean getcreationPage(){
		return this.creationPage;
	}

	public String getNamePayer() {
		return namePayer;
	}

	public void setNamePayer(String namePayer) {
		this.namePayer = namePayer;
	}

	public String getMessagePayer() {
		return messagePayer;
	}

	public void setMessagePayer(String messagePayer) {
		this.messagePayer = messagePayer;
	}

	public int getJoinedPeople() {
		return joinedPeople;
	}

	public void setJoinedPeople(int joinedPeople) {
		this.joinedPeople = joinedPeople;
	}
	
	public BasePackageDTO getBasePackage() {
		return basePackage;
	}

	public void setBasePackage(BasePackageDTO basePackage) {
		this.basePackage = basePackage;
	}

	public int getExcToBePaid() {
		return excToBePaid;
	}

	public void setExcToBePaid(int excToBePaid) {
		this.excToBePaid = excToBePaid;
	}
}
